#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;

// 基于等间隔提取图像缩放
cv::Mat imageReduction(cv::Mat &srcImage, float kx, float ky) {
    // 获取输出图像分辨率
    int nRows = cvRound(srcImage.rows * kx);
    int nCols = cvRound(srcImage.cols * ky);

    cv::Mat resultImage(nRows, nCols, srcImage.type());
    for (int i = 0; i < nRows; i++) {
        // 根据水平因子计算坐标
        for (int j = 0; j < nCols; j++) {
            int x = static_cast<int>((i + 1) / kx + 0.5) - 1;   // 运算符可用于将指向基类的指针转换为指向派生类的指针 static_cast 等操作
            int y = static_cast<int>((j + 1) / ky + 0.5) - 1;

            resultImage.at<cv::Vec3b>(i, j) = srcImage.at<cv::Vec3b>(x,
                                                                     y); //vector(向量): C++中的一种数据结构,一个类，相当于一个动态的数组。（类模板）
        }

    }

    return resultImage;
}

cv::Vec3b areaAverage(const cv::Mat &srcImage, Point_<int> leftPoint, Point_<int> rightPoint) {
    int temp1 = 0, temp2 = 0, temp3 = 0;
    // 计算区域子块像素点个数
    int nPix = (rightPoint.x - leftPoint.x + 1) * (rightPoint.y - leftPoint.y + 1);

    // 对区域字块各通道对像素值求和
    for (int i = leftPoint.x; i <= rightPoint.x; i++) {
        for (int j = leftPoint.y; j <= rightPoint.y; j++) {
            temp1 += srcImage.at<cv::Vec3b>(i, j)[0];
            temp2 += srcImage.at<cv::Vec3b>(i, j)[1];
            temp3 += srcImage.at<cv::Vec3b>(i, j)[2];
        }
    }

    // 对每个通道求平均值
    Vec3b vecTemp;
    vecTemp[0] = temp1 / nPix;
    vecTemp[1] = temp2 / nPix;
    vecTemp[2] = temp3 / nPix;
    return vecTemp;
}

int main() {
    cv::Mat srcImage = cv::imread("/Users/chris/Documents/python-learning-team/杨子萱/example_opencv3_cpp/image/test.jpg");
    if (!srcImage.data) return -1;
    cv::imshow("srcImage", srcImage);
    std::cout<< srcImage.rows <<std::endl;

    cv::Mat resultImage1 = imageReduction(srcImage, 0.5, 0.5);
    cv::imshow("res1", resultImage1);
    std::cout<< resultImage1.rows <<std::endl;
    cv::waitKey(0);
    return 0;
}